package com.freshcity.core
{
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.guide.GuideTipsManager;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.support.MaskInfo;
	import com.freshcity.page.common.UserDetail;
	import com.freshcity.page.common.UserHome;
	import com.freshcity.page.feedback.FeedBack;
	import com.freshcity.page.feiji.FeijiHome;
	import com.freshcity.page.friend.FriendHome;
	import com.freshcity.page.help.HelpHome;
	import com.freshcity.page.home.CallListPanel;
	import com.freshcity.page.home.FriendListPanel;
	import com.freshcity.page.home.MainNav;
	import com.freshcity.page.home.MessagePanel;
	import com.freshcity.page.home.ReadMessage;
	import com.freshcity.page.home.StatusPanel;
	import com.freshcity.page.home.WriteMessage;
	import com.freshcity.page.search.LatestUserList;
	import com.freshcity.page.search.RequestSelect;
	import com.freshcity.page.search.SearchHome;
	import com.freshcity.page.talk.Talk;
	import com.freshcity.page.tree.TreeHome;
	import com.freshcity.page.user.RegisterQQ;
	import com.freshcity.page.user.SelfHome;
	import com.freshcity.page.wait.RoomList;
	import com.freshcity.page.welcome.FriendCond;
	import com.freshcity.page.welcome.Register;
	import com.freshcity.utils.RandomUtil;
	import com.freshcity.utils.StringUtil;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.pblabs.debug.Logger;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class MainScene extends BasePage
	{
		public var selfHome:SelfHome;
		public var feijiHome:FeijiHome;

		public var statusPanel:StatusPanel;
		public var roomList:RoomList;
		public var talk:Talk;
		public var feedback:FeedBack;
		public var friendPanel:FriendListPanel;

		public static var userInfoHome:UserHome;
		public static var friendInfoHome:FriendHome;
		
		public static var writeMsg:WriteMessage;
		public static var reqTalk:RequestSelect;

		private var readMsg:ReadMessage;

		private var messages:Array;
		private var msgPanel:MessagePanel;
		private var callListPanel:CallListPanel;

		public var currentScene:BaseHomeWin;
		public var treeHome:TreeHome;
		public var helpHome:HelpHome;
		public var searchHome:LatestUserList;
		public var qqCallback:Function;
		
		public var usersDict:Dictionary=new Dictionary();
		public var treesDict:Dictionary=new Dictionary();
		public var mainNav:MainNav;

		public function MainScene(_name:String=null)
		{
			super(_name);
		}

		override public function destroy():void
		{
			super.destroy();
		}

		override public function initialize():void
		{
			super.initialize();
			Logger.debug(this, "stage width is " + String(stage.width), ", height is " + String(stage.height) + " at " + StringUtil.getTime());

			//初始化主导航
			mainNav=new MainNav();
			mainNav.main=this;
			LayerManager.sceneLayer.addChildAt(mainNav, 0);
			mainNav.show();

			var bgImgUrls:Array=["01.jpg", "02.jpg","03.jpg", "04.jpg","05.jpg", "06.jpg"];
			var bgUrl:String=RandomUtil.randomValue(bgImgUrls) as String;

			var loader:ImageLoader=new ImageLoader(GlobalVar.MAIN_PATH 
				+ "/img/background/" 
				+ bgUrl + "?v=" 
				+ GlobalVar.version, {y:50, onComplete: loadCompleteHandler});
			loader.load();
			
			if (this.user.level <= 0)
			{
				var reg:Register=new Register();
				reg.main=this;
				this.user.level=0;
				reg.show();
			}
		}

		private function loadCompleteHandler(event:LoaderEvent):void
		{
			var loc1:DisplayObject=event.target.content;
			LayerManager.sceneLayer.addChildAt(loc1, 0);
		}

		//加载后构建
		public function postInit():void
		{
			statusPanel=new StatusPanel();
			statusPanel.owningGroup=this;
			statusPanel.initialize();

			msgPanel=new MessagePanel();
			msgPanel.owningGroup=this;
			msgPanel.initialize();

			callListPanel=new CallListPanel();
			callListPanel.owningGroup=this;
			callListPanel.initialize();

			friendPanel=new FriendListPanel();
			friendPanel.owningGroup=this;
			friendPanel.initialize();

			userInfoHome=new UserHome();
			userInfoHome.main=this;
			
			friendInfoHome = new FriendHome();
			friendInfoHome.main=this;

			writeMsg=new WriteMessage();
			writeMsg.main=this;

			readMsg=new ReadMessage();
			readMsg.main=this;
		}

		public function getQQInfo(callback:Function):void
		{
			this.qqCallback=callback;
			this.settingCallback=registerQQNo;

			if (user.qqNo == '')
			{
				var htmlContent:String="通过交换QQ号码认识有缘人。只有符合条件的人才能申请与你交换QQ号，在登记QQ号码前需要设置择友条件。确认继续吗？";
				var _confirm:Function=function(arg1:int):void
				{
					if (arg1 == Alert.OK)
					{
						if (cond.ageFrom == undefined)
						{
							showFriendSetting();
						}
						else
						{
							registerQQNo();
						}
					}
				};
				Alert.show(htmlContent, "", Alert.OK | Alert.CANCEL, null, _confirm);
			}
			else
			{
				callback();
			}
		}

		public function showFriendSetting():void
		{
			var setting:FriendCond=new FriendCond();
			setting.main=this;
			setting.show();
		}

		public function registerQQNo():void
		{
			var reg:RegisterQQ=new RegisterQQ();
			reg.main=this;
			reg.show();
		}

		public function initSelf():void
		{
			selfHome=new SelfHome(this);
		}

		public function initFeiji():void
		{
			feijiHome=new FeijiHome();
			feijiHome.main=this;
		}

		public function initTree():void
		{
			treeHome=new TreeHome(this);
		}

		public function initSearch():void
		{
			searchHome=new LatestUserList(this.user.sex);
			searchHome.main=this;
		}

		public function initHelp():void
		{
			helpHome=new HelpHome();
			helpHome.main=this;
		}

		public function initRoomList():void
		{
			if (!roomList)
			{
				roomList=new RoomList();
				roomList.owningGroup=this;
				roomList.initialize();
			}
		}

		public function initTalk():void
		{
			if (!talk)
			{
				talk=new Talk();
				talk.main=this;
			}
		}

		public function initFeedBack():void
		{
			if (!feedback)
			{
				feedback=new FeedBack();
				feedback.owningGroup=this;
				feedback.initialize();
			}
		}

		public static function findChild(name:String, displayObjectToSearch:DisplayObject=null):DisplayObject
		{
			return _findChild(name, displayObjectToSearch);
		}

		public function showMessage(data:Array):void
		{
			readMsg.data=data;
		}

		public function onAboutUser(event:TextEvent):void
		{
			var user:String=event.text;
			Dispatcher.addEventListener(EventName.UserReadDone,showUser);
			findUser(user);
		}

		public function showUser(evt:DataEvent):void
		{
			Dispatcher.removeEventListener(EventName.UserReadDone,showUser);
			EffectManager.hideLoadMask();
			var data:* = evt.data;
			if(this.friends[data.user.id]!=null&&this.friends[data.user.id].level>0){
				MainScene.friendInfoHome.asso=this.friends[data.user.id];
			}else{
				MainScene.userInfoHome.data=data;
			}
		}
		
		public function findUser(uid:String):void
		{
			EffectManager.addLoadMask();
			if(usersDict[uid]){
				//MainScene.userInfoHome.data=usersDict[uid];
				Dispatcher.dispatchEvent(new DataEvent(EventName.UserReadDone,usersDict[uid]));
			}else{
				this.commu.send(OpCodes.READ_USER, {u: uid}, readUserCallback);
			}
		}
		
		private function readUserCallback(d:*):void
		{
			this.commu.unRegister(OpCodes.READ_USER);
			usersDict[d.user.id] = d;
			//MainScene.userInfoHome.data=d;
			//MainScene.userInfoDetail.viewModel=true;
			Dispatcher.dispatchEvent(new DataEvent(EventName.UserReadDone,d));
		}
		
		public function findTree(uid:String):void
		{
			EffectManager.addLoadMask();
			if(treesDict[uid]){
				Dispatcher.dispatchEvent(new DataEvent(EventName.TreeReadDone,treesDict[uid]));
			}else{
				this.commu.send(2408, {u: uid}, showTreeInfo);
			}
		}
		
		private function showTreeInfo(data:*):void
		{
			this.commu.unRegister(2408);
			treesDict[data.owner] = data;
			Dispatcher.dispatchEvent(new DataEvent(EventName.TreeReadDone,data));
		}

		private static function _findChild(name:String, current:DisplayObject):DisplayObject
		{
			if (!current)
				return null;

			if (current.name == name)
				return current;

			var parent:DisplayObjectContainer=current as DisplayObjectContainer;

			if (!parent)
				return null;

			for (var i:int=0; i < parent.numChildren; i++)
			{
				var child:DisplayObject=_findChild(name, parent.getChildAt(i));
				if (child)
					return child;
			}

			return null;
		}
	}
}